<template>
  <el-dialog title="次月额度发放" :visible.sync="isShow" @close="$emit('update:isShowGrant', false)" width="500px" :close-on-click-modal="false">
    <el-form label-position="right" ref="info" :rules="rules" :model="info" label-width="120px">
      <el-form-item label="企业名称:" prop="status">
        <span>{{info.enterpriseName}}</span>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="本次发放额度:" prop="credit">
            <el-input v-model="info.credit" style="min-width: 140px">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账期:" prop="count">
            <el-select v-model="info.count" placeholder="全部">
              <el-option v-for="n in countLimit" :key="n" :label="n + '个月'"
                         :value="n"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="发放备注:" prop="remark">
        <el-input type="textarea" v-model="info.remark"
                  :autosize="{ minRows: 3, maxRows: 8}" placeholder="请输入内容"></el-input>
      </el-form-item>
    </el-form>
    <div class="footer text-center mt20">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="showSecondaryAlert">确定发放</el-button>
    </div>
    <SecondaryConfirm
      @confirmAmout="confirmAmout"
      :isShowDetail.sync="isShowSecondary"
      :dataInfo="info"
      :type.sync="secondaryType"></SecondaryConfirm>
  </el-dialog>
</template>

<script>
  import SecondaryConfirm from './SecondaryConfirm'
  export default {
    props: {
      isShowGrant: {
        type: Boolean,
        default: false
      },
      grantInfo: {
        type: Object,
        default: () => {
          return {
            id:"",
            enterpriseName:"",
            credit:""
          }
        }
      }
    },
    components: { SecondaryConfirm },
    data() {
      return {
        isShow: this.isShowGrant,
        info: {...this.grantInfo,remark:"",count:0},
        secondaryType: 0,
        countLimit:0,
        isShowSecondary: false,
        rules: {
          credit: [{required: true, message: '请输入发放额度', trigger: 'blur'},
            {validator: validate.twoDecimal, trigger: 'change'}],
          remark: [
            {required: true, message: '请输入发放备注', trigger: 'blur'},
            {validator: validate.length150, trigger: 'change'}
          ]
        },
      }
    },
    watch: {
      isShowGrant(val) {
        this.isShow = val
      },
      grantInfo(val) {
        this.info = {...val,remark:"",count:0}
        this.getSchemaLimit(val.id)
      }
    },
    methods: {
      onSubmit() {
        this.grant()
      },
      showSecondaryAlert() {
        this.$refs['info'].validate((valid) => {
          if (valid) {
            if(this.info.count < 1){
              this.$message({
                message: "请选择账期",
                type: 'error'
              });
            }else {
              this.isShowSecondary = true
            }
          }
          else {
            return false;
          }
        });
      },
      grant() {
        let info = {
          id:this.info.id,
          credit:this.info.credit,
          count:this.info.count,
          remark:this.info.remark
        }
        ajax.post(apis.factory.creditPass, info)
          .then(res => {
            if (res.success === true) {
              this.$message({
                message: res.message,
                type: 'success'
              });
              this.isShow = false
              this.isShowSecondary = false;
              this.$emit("refreshCurrentPage")
            } else {
              this.$message({
                message: res.message,
                type: 'error'
              })
            }
          })
      },
      confirmAmout() {
        this.onSubmit()
      },
      // 账期最大值获取
      getSchemaLimit(id) {
        ajax.get(apis.factory.getSchemaLimit, {id:id})
          .then(res => {
            if (res.success === true) {
              this.countLimit = res.data
              if(this.countLimit > 0){
                this.$set(this.info,"count",1)
              }
            }else{
              this.countLimit = 0
            }
          })
      }
    },
    created() {
    }
  }
</script>

<style lang="scss" scoped>
</style>
